package com.supplychainapi.controller;

import com.joindata.inf.common.basic.exceptions.ServiceException;
import com.supplychainapi.common.ApiVersion;
import com.supplychainapi.common.RestResponse;
import com.supplychainapi.domain.LoginCache;
import com.supplychainapi.model.ScmTbdeliveraddr;
import com.supplychainapi.param.DeliverAddrParam;
import com.supplychainapi.service.IScmTbDeliverAddrService;
import com.supplychainapi.utils.PathUitls;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;

/**
 * Created by panic .
 * Time 2017/11/27
 * description:地址管理
 */

@RequestMapping(PathUitls.ROOT_PHONE + "/{version}/address")
@RestController
@Api(value = "DeliverAddrController", description = "地址管理")
public class DeliverAddrController extends BaseController {
    private Logger logger = LogManager.getLogger(this.getClass());
    @Autowired
    IScmTbDeliverAddrService tbDeliverAddrService;

    @ApiVersion(1)
    @ApiOperation(value = "新增地址", notes = "")
    @RequestMapping(value = "/saveAddress", method = RequestMethod.POST)
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "fiaddrid", value = "地址id;(新增时任意传参)", required = true, dataType = "Integer", defaultValue = ""),
            @ApiImplicitParam(paramType = "query", name = "fsaddress", value = "地址;swagger", required = true, dataType = "String", defaultValue = ""),
            @ApiImplicitParam(paramType = "query", name = "fsreceiver", value = "收货人;swagger", required = true, dataType = "String", defaultValue = ""),
            @ApiImplicitParam(paramType = "query", name = "fscellphonerr", value = "收货人手机;swagger", required = true, dataType = "String", defaultValue = ""),
            @ApiImplicitParam(paramType = "query", name = "fidefault", value = "是否默认;0=否/1=是", required = true, dataType = "String", defaultValue = ""),
    })
    public RestResponse saveAddress(@ApiIgnore LoginCache loginCache, @ApiIgnore @RequestBody DeliverAddrParam model) throws ServiceException {
        return tbDeliverAddrService.createAddr(loginCache, model);
    }

    @ApiVersion(1)
    @ApiOperation(value = "更新地址", notes = "")
    @RequestMapping(value = "/updateAddr", method = RequestMethod.POST)
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "fiaddrid", value = "地址id(更新时必传);swagger", required = true, dataType = "String", defaultValue = ""),
            @ApiImplicitParam(paramType = "query", name = "fsaddress", value = "地址;swagger", required = true, dataType = "String", defaultValue = ""),
            @ApiImplicitParam(paramType = "query", name = "fsreceiver", value = "收货人;swagger", required = true, dataType = "String", defaultValue = ""),
            @ApiImplicitParam(paramType = "query", name = "fscellphonerr", value = "收货人手机;swagger", required = true, dataType = "String", defaultValue = ""),
            @ApiImplicitParam(paramType = "query", name = "fidefault", value = "是否默认;0=否/1=是", required = true, dataType = "String", defaultValue = ""),
    })
    public RestResponse updateAddr(@ApiIgnore LoginCache loginCache, @ApiIgnore @RequestBody DeliverAddrParam deliverAddrParam) throws ServiceException {
        return tbDeliverAddrService.updateAddr(loginCache, deliverAddrParam);
    }

    @ApiVersion(1)
    @ApiOperation(value = "查询所有地址", notes = "")
    @RequestMapping(value = "/selectAllAddress", method = RequestMethod.GET)
    public RestResponse<List<ScmTbdeliveraddr>> selectAllAddress(@ApiIgnore LoginCache loginCache) throws ServiceException {
        return tbDeliverAddrService.retrieveAllAddr(loginCache.getFsShopGUID());
    }

    @ApiVersion(1)
    @ApiOperation(value = "删除一条地址", notes = "")
    @RequestMapping(value = "/deleteAddr", method = RequestMethod.GET)
    public RestResponse deleteAddr(@ApiIgnore LoginCache loginCache, @RequestParam Integer addrId) throws ServiceException {
        return tbDeliverAddrService.deleteAddr(loginCache.getFsShopGUID(), addrId);
    }

    @ApiVersion(1)
    @ApiOperation(value = "设置默认地址", notes = "")
    @RequestMapping(value = "/setDefaultAddress", method = RequestMethod.GET)
    public RestResponse setDefaultAddress(@RequestParam Integer addressId, @ApiIgnore LoginCache loginCache) throws ServiceException {
        return tbDeliverAddrService.setDefaultAddress(addressId, loginCache.getFsShopGUID());
    }

    @ApiVersion(1)
    @ApiOperation(value = "获取默认地址", notes = "")
    @RequestMapping(value = "/getDefaultAddress", method = RequestMethod.GET)
    public RestResponse getDefaultAddress(@ApiIgnore LoginCache loginCache) throws ServiceException {
        return tbDeliverAddrService.getDefaultAddress(loginCache.getFsShopGUID());
    }


}
